package com.sxtyxt.online;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;

import com.sxtyxt.entity.User;
import com.sxtyxt.util.ToastUtil;

import java.io.Serializable;

public class BaseFragment extends Fragment {

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void transfer(Class<?> clazz) {
        try {
            Intent intent = new Intent(getActivity(), clazz);
            startActivityForResult(intent, 0);
            // overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
        } catch (Exception e) {
            doToast("该功能尚未开发,敬请期待");
            Log.e("transfer", e.toString());
        }

    }

    public void transfer(Class<?> clazz, boolean flag, String params) {
        try {
            Intent intent = new Intent(getActivity(), clazz);
            intent.putExtra(params, flag);
            startActivityForResult(intent, 0);
            // overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
        } catch (Exception e) {
            doToast("该功能尚未开发,敬请期待");
            Log.e("transfer", e.toString());
        }
    }

    public void transfer(Class<?> clazz, Serializable obj, String params) {
        try {
            Intent intent = new Intent(getActivity(), clazz);
            intent.putExtra(params, obj);
            startActivityForResult(intent, 0);
            // overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
        } catch (Exception e) {
            doToast("该功能尚未开发,敬请期待");
            Log.e("transfer", e.toString());
        }
    }


    public void transfer(Class<?> clazz, int requestCode) {
        try {
            Intent intent = new Intent(getActivity(), clazz);
            startActivityForResult(intent, requestCode);
        } catch (Exception e) {
            doToast("该功能尚未开发,敬请期待");
            Log.e("transfer", e.toString());
        }
    }


    public void transfer(Class<?> clazz, String str, String params,
                         int requestCode) {
        try {
            Intent intent = new Intent(getActivity(), clazz);
            intent.putExtra(params, str);
            startActivityForResult(intent, requestCode);
        } catch (Exception e) {
            doToast("该功能尚未开发,敬请期待");
            Log.e("transfer", e.toString());
        }
    }

    public void transfer(Class<?> clazz, int requestCode, Serializable obj,
                         String params) {
        try {
            Intent intent = new Intent(getActivity(), clazz);
            intent.putExtra(params, obj);
            startActivityForResult(intent, requestCode);
        } catch (Exception e) {
            doToast("该功能尚未开发,敬请期待");
            Log.e("transfer", e.toString());
        }
    }

    public static String getUnNullString(String s, String defalt) {
        return (s == null || TextUtils.isEmpty(s) || "null".equals(s)) ? defalt : s;
    }

    public final User getLoginUser() {
        return App.getInstance().getLoginUser();
    }

    public final String getLoginUserId() {
        User user = getLoginUser();
        if (user == null)
            return "";
        return user.getPkId();
    }

    public void doToast(String string){
        ToastUtil.getInstance().showToast(getActivity(),string);
    }

    public void doToast(int resId){
        ToastUtil.getInstance().showToast(getActivity(), resId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ToastUtil.getInstance().cancleToast();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ToastUtil.getInstance().cancleToast();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        ToastUtil.getInstance().cancleToast();
    }
}
